// ----------------
// Project:
// ESA Elevator
// ----------------
//
// ----------------
// Group: 163
// 2358569: Pascal Klink
// 2649456: Lukas Kreh
// ----------------
//
// Description:
// ----------------
// elevator sensor-interface
//
// Version History:
// ----------------
// 140116: changed unit in comment of VELOCITY_THRESHOLD

`timescale 1ns / 1ns

module sensor_if

          #(parameter WEIGHT_BITS        =   10,  // weight sensor input bits
                      MAX_WEIGHT         =  500,  // maximum weight [kg]
                      VELOCITY_BITS      =   11,  // velocity sensor input bits
                      VELOCITY_THRESHOLD = 2000)  // velocity threshold [cm/s]


          (input  wire                          CLK,
           input  wire                          RESET,

           input  wire                          DOOR_LIGHT_CURTAIN_IN,         // 1 if object in the way
           input  wire [(VELOCITY_BITS-1)   :0] VELOCITY_SENSOR_IN,            // speed [cm/s]
           input  wire [(WEIGHT_BITS-1)     :0] WEIGHT_SENSOR_IN,              // weight [kg]
           input  wire                          SMOKE_PARTICLE_SENSOR_IN,      // 1 if smoke detected
           
           output reg                           DOOR_LIGHT_CURTAIN_OUT,        // 0 if door is free
           output reg                           VELOCITY_SENSOR_OUT,           // 1 if speed is ok
           output reg                           WEIGHT_SENSOR_OUT,             // 1 if weight is ok
           output reg                           SMOKE_PARTICLE_SENSOR_OUT);    // 0 if smoke is ok

/* =============================INSERT CODE HERE======================================*/ 
reg [7:0] door_count;	//back to 8 bits
reg [7:0] speed_count=0;
reg [7:0] weight_count=0;
reg [7:0] smoke_count=0;
always@(posedge CLK)begin
	//reset
	if(RESET)begin
		door_count<=0;
		speed_count<=0;
		weight_count<=0;
		smoke_count<=0;
		DOOR_LIGHT_CURTAIN_OUT<=0;
		VELOCITY_SENSOR_OUT<=1;
		WEIGHT_SENSOR_OUT<=1;
		SMOKE_PARTICLE_SENSOR_OUT<=0;
	end
	else begin
		//if sensors are activated for more than 255clks, save the signal
		//check for door
		if(DOOR_LIGHT_CURTAIN_IN)begin
			//set door_out
			if(door_count==255)begin
				DOOR_LIGHT_CURTAIN_OUT<=1;
			end
			else begin
				door_count<=door_count+1;
			end
		end
		else begin
			//reset counter and output
			door_count<=0;
			DOOR_LIGHT_CURTAIN_OUT<=0;
		end 
		//same here for smoke.. 
		if(SMOKE_PARTICLE_SENSOR_IN)begin
			if(smoke_count==255)begin
				SMOKE_PARTICLE_SENSOR_OUT<=1;
			end
			else begin
				smoke_count<=smoke_count+1;
			end
		end
		else begin
			smoke_count<=0;
			SMOKE_PARTICLE_SENSOR_OUT<=0;
		end
		//velocity..
		if(VELOCITY_SENSOR_IN>=VELOCITY_THRESHOLD)begin
			if(speed_count==255)begin
				VELOCITY_SENSOR_OUT<=0;
			end 
			else begin
				speed_count<=speed_count+1;
			end
		end 
		else begin 
			speed_count<=0;
			VELOCITY_SENSOR_OUT<=1;
		end
		//weight...
		if(WEIGHT_SENSOR_IN>=MAX_WEIGHT)begin
			if(weight_count>=255)begin
				WEIGHT_SENSOR_OUT<=0;
			end
			else begin
				weight_count<=weight_count+1;
			end
		end
		else begin
			weight_count<=0;
			WEIGHT_SENSOR_OUT<=1;
		end
	end
end

/* ====================================================================================*/

endmodule
